import java.sql.*;
import java.util.Scanner;

public class Test01 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        test04();
    }

    private static void test04() throws ClassNotFoundException, SQLException {
        Scanner scanner=new Scanner(System.in);
        String className = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql:///db2";
        String user = "root";
        String password="123";
        Class.forName(className);
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println("输入姓名");
        String sname=scanner.nextLine();
        System.out.println("输入密码");
        String pwd=scanner.nextLine();
        String sql="select count(*) from student where sname=? and password=?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1, sname);
        statement.setString(2, pwd);
        ResultSet resultSet = statement.executeQuery();
        if(resultSet.next()){
            int count=resultSet.getInt(1);
            if(count>0){
                System.out.println("登录成功");
            }else{
                System.out.println("登录失败");
            }
        }
        statement.close();
        connection.close();
    }
    private static void test03() throws ClassNotFoundException, SQLException {
        Scanner scanner=new Scanner(System.in);
        String className = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql:///db2";
        String user = "root";
        String password="123";
        Class.forName(className);
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println("输入姓名");
        String sname=scanner.nextLine();
        System.out.println("输入密码");
        String pwd=scanner.nextLine();
        Statement statement = connection.createStatement();
        String sql="select count(*) from student where sname='"+sname+"' and password='"+pwd+"'";
        System.out.println(sql);
        ResultSet resultSet = statement.executeQuery(sql);

        if(resultSet.next()){
            int count=resultSet.getInt(1);
            if(count>0){
                System.out.println("登录成功");
            }else{
                System.out.println("登录失败");
            }
        }
        statement.close();
        connection.close();
    }

    private static void test02() throws ClassNotFoundException, SQLException {
        Scanner scanner=new Scanner(System.in);
        String className = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql:///db2";
        String user = "root";
        String password="123";
        Class.forName(className);
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println("输入姓名");
        String sname=scanner.next();
        System.out.println("输入年龄");
        int age= Integer.parseInt(scanner.next());
        System.out.println("输入性别");
        String gender=scanner.next();
        //使用PreparedStatement参数需要指定sql语句,
        //sql中变量的值不需要使用字符串拼接的方式 ++++，支持?占位符
        String sql="insert into stu values (null,?,?,?)";
        PreparedStatement statement = connection.prepareStatement(sql);
        //给占位符准备具体的数据
        statement.setString(1,sname);
        statement.setInt(2,age);
        statement.setString(3,gender);
        System.out.println("执行的sql语句:"+sql);
        int row = statement.executeUpdate();
        System.out.println("受影响的行数:"+row);
        statement.close();
        connection.close();
    }
    private static void test01() throws ClassNotFoundException, SQLException {
        Scanner scanner=new Scanner(System.in);
        String className = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql:///db2";
        String user = "root";
        String password="123";
        Class.forName(className);
        Connection connection = DriverManager.getConnection(url, user, password);
        Statement statement = connection.createStatement();
        System.out.println("输入姓名");
        String sname=scanner.next();
        System.out.println("输入年龄");
        int age= Integer.parseInt(scanner.next());
        System.out.println("输入性别");
        String gender=scanner.next();
        String sql="insert into stu values (null,'"+sname+"',"+age+",'"+gender+"')";
        System.out.println("执行的sql语句:"+sql);
        int row = statement.executeUpdate(sql);
        System.out.println("受影响的行数:"+row);
        statement.close();
        connection.close();
    }
}
